package cn.fintecher.pangolin.service.manage.web;

import cn.fintecher.pangolin.common.web.BaseController;
import cn.fintecher.pangolin.entity.mysql.SysParam;
import cn.fintecher.pangolin.service.manage.repository.SysParamRepository;
import cn.fintecher.pangolin.service.manage.request.FindSysParamRequest;
import cn.fintecher.pangolin.service.manage.request.ModifySysParamRequest;
import cn.fintecher.pangolin.service.manage.response.SysParamResponse;
import cn.fintecher.pangolin.service.manage.service.SysParamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @Auther: xiaqun
 * @Description: 系统参数相关接口
 * @Date: 18:18 2019/8/14
 */

@RestController
@RequestMapping("/api/sysParamController")
@Api(value = "系统参数相关接口", description = "系统参数相关接口")
public class SysParamController extends BaseController {

    @Autowired
    private SysParamService sysParamService;

    @Autowired
    private SysParamRepository sysParamRepository;

    /**
     * 修改系统参数
     */
    @ApiOperation(value = "修改系统参数", notes = "修改系统参数")
    @PutMapping("/modifySysParam")
    public ResponseEntity<Void> modifySysParam(@Valid @RequestBody ModifySysParamRequest request,
                                               @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to modify sysParam,{}", request);
        sysParamService.modifySysParam(request, token);
        return ResponseEntity.ok().build();
    }

    /**
     * 多条件查询系统参数
     */
    @ApiOperation(value = "多条件查询系统参数", notes = "多条件查询系统参数")
    @GetMapping("/findAllSysParam")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query",
                    value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query",
                    value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query",
                    value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<SysParamResponse>> findAllSysParam(FindSysParamRequest request, Pageable pageable) {
        log.info("REST request to fin all sysParam,{}", request);
        Page<SysParamResponse> page = sysParamService.findAllSysParam(request, pageable);
        return ResponseEntity.ok().body(page);
    }

    @ApiOperation(value = "查询参数", notes = "查询参数")
    @GetMapping(value = "/querySysParamByCode")
    public ResponseEntity<SysParam> querySysParamByCode(@RequestParam String code) {
        log.debug("REST request to querySysParamByCode : {}", code);
        SysParam sysParam = sysParamRepository.findByCode(code);
        return ResponseEntity.ok().body(sysParam);
    }
}
